<route>
{
  "meta":{
    "title":"定时任务",
    "showMenu":true,
    "serialNum": 10,
    "localism": "sys.scheduled_task"
  }
}
</route>

<template>
  <div>
    <el-row :gutter="12">
      <el-col :span="6">
        <el-input
          v-model="queryData.name"
          :placeholder="$t('sys.task_name')"
          clearable
          :maxlength="64"
          prefix-icon="el-icon-search"
        />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="loadData">{{$t('sys.query')}}</el-button>
        <el-button @click="handleReset">{{$t('sys.reset')}}</el-button>
      </el-col>
      <el-col v-if="VUE_APP_USER !== 'APP'" class="mt10">
        <el-button type="primary" @click="handleCreate">{{$t('sys.new')}}</el-button>
      </el-col>
    </el-row>
    <el-table
      class="mt10"
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      current-row-key="id"
      stripe
      border
    >
      <el-table-column prop="jobName" :label="$t('sys.task_name')" />
      <el-table-column prop="cronExpression" :label="$t('sys.cron_expressions')" />
      <el-table-column prop="scriptName" :label="$t('sys.script_name')" />
      <el-table-column prop="status" :label="$t('sys.status')">
        <template slot-scope="scope">
          {{ scope.row.status !== "0" ? $t("sys.enable") : $t("sys.disable") }}
        </template>
      </el-table-column>
      <el-table-column prop="modifyDate" :label="$t('sys.update_time')">
        <template slot-scope="scope">
          {{ scope.row.modifyDate | moment }}
        </template>
      </el-table-column>

      <el-table-column prop="actions" :label="$t('sys.operation')">
        <template slot-scope="scope">
          <template>
            <span v-if="VUE_APP_USER !== 'APP'" class="table-actionItem">
              <el-button type="text" @click="handleEdit(scope.row)"
                >{{$t('sys.edit')}}</el-button
              >
            </span>
            <span
              v-if="scope.row.scriptId && VUE_APP_USER !== 'APP'"
              class="table-actionItem"
            >
              <el-button type="text" @click="handleDebugScript(scope.row)"
                >{{$t('sys.script_debug')}}</el-button
              >
            </span>
            <span v-if="VUE_APP_USER !== 'APP'" class="table-actionItem">
              <el-popconfirm
                :title="$t('sys.confirm_to_delete_task')"
                placement="top"
                @confirm="handleDelete(scope.row)"
              >
                <el-button class="link-danger" type="text" slot="reference"
                  >{{$t('sys.delete')}}</el-button
                >
              </el-popconfirm>
            </span>
            <span v-if="VUE_APP_USER === 'APP'" class="table-actionItem">
              <el-button type="text" @click="handleView(scope.row)"
                >{{$t('sys.view')}}</el-button
              >
            </span>
            <span
              v-if="VUE_APP_USER === 'APP' && scope.row.status === '0'"
              class="table-actionItem"
            >
              <el-popconfirm
                :title="$t('sys.confirm_to_enable_task')"
                placement="top"
                @confirm="changeTaskStatus(scope.row, 1)"
              >
                <el-button type="text" slot="reference">{{$t('sys.enable')}}</el-button>
              </el-popconfirm>
            </span>
            <span
              v-if="VUE_APP_USER === 'APP' && scope.row.status === '1'"
              class="table-actionItem"
            >
              <el-popconfirm
                :title="$t('sys.confirm_to_disable_task')"
                placement="top"
                @confirm="changeTaskStatus(scope.row)"
              >
                <el-button class="link-danger" type="text" slot="reference"
                  >{{$t('sys.disable')}}</el-button
                >
              </el-popconfirm>
            </span>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <div class="tr mt24">
      <x-pagination
        :total="total"
        :pageSize.sync="pageData.pageSize"
        :pageNo.sync="pageData.pageNo"
        @search="loadData"
      />
    </div>

    <JobForm ref="JobForm" @ok="loadData" />
  </div>
</template>

<script>
import JobForm from "./__components__/JobForm";
import { getHmjobPageList,deleteHmjob,putHmjobChangeStatus } from '@/apis/paas/HmJobController.js'
export default {
  name: "Job",
  components: { JobForm },
  data() {
    return {
      VUE_APP_USER: process.env.VUE_APP_USER,
      loading: false,
      queryData: {},
      pageData: {
        pageSize: 10,
        pageNo: 1,
      },
      tableData: [],
      total: 0,
    };
  },

  created() {
    this.loadData();
  },

  methods: {
    loadData() {
      getHmjobPageList({
        ...this.queryData,
        ...this.pageData,
      }).then((res) => {
        this.tableData = res.data;
        this.total = res.totalCount;
      });
    },

    handleReset() {
      this.queryData = {};
      this.loadData();
    },

    handleCreate() {
      this.$refs.JobForm.addForm();
    },
    // 查看
    handleView(record) {
      this.$refs.JobForm.viewForm({
        ...record,
      });
    },
    handleEdit(record) {
      this.$refs.JobForm.editForm({
        ...record,
      });
    },

    handleDelete(record) {
      deleteHmjob([record.jobId]).then(() => {
        this.loadData();
        this.$message.success(this.$t("sys.operation_success"));
      });
    },
    // 脚本调试
    handleDebugScript(record) {
      const { origin, pathname } = window.location;
      window.open(
        `${origin}${pathname}#/scriptDebug?scriptId=${record.scriptId}`
      );
    },
    changeTaskStatus(record, flag) {
      putHmjobChangeStatus({ jobId: record.jobId }).then(() => {
        this.loadData();
        this.$message.success(this.$t("sys.operation_success"));
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
